package com.uusee.crawler.pageprocessor.baike.mtime;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.bk.Constants;
import com.uusee.shipshape.bk.model.Baike;
import com.uusee.shipshape.bk.model.BaikeImage;

public class MoviePostersAndImagesPageProcessor extends Processor {	
	private static final String MODEL_LIST_REG="<div class=\"movie_img_list[^<]*?>[^<]*?<ul class=\"clearfix\">(.*)</ul>.*?<div id=\"movie_main_r\" class=\"fr\">";
	private static final String MODEL_REG="<li>(.*?)</li>";
	
	private static final String ORI_IMAGE_URL_REG="<img.*?src=\"(.*?)\"/>";
	private static final String NAME_REG="<img.*?/></a>	<p>(.*?)</p>";
	private static final String CATEGORY_REG="<img.*?/></a>	<p>(\\w*?)\\W.*?</p>";
	private static final String MEASURE_REG="<p class=\"px11 c_8e\">(.*?)</p>";
	private static final String SIZE_REG="<p class=\"px11 c_8e mt-3\">(.*?)</p>";
	
	private String domainReg = "http://(.*?)/";
	
	protected void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}

		try {
			Date crawlDate = new Date();
			String crawlUser = "uusee-crawler";
			String crawlUrl = crawlURI.getCrawlUrl();
			String oriId = PageProcessorUtils.getValue("http://www.mtime.com/movie/([0-9]+)/", crawlUrl);
			String sourceSite = crawlURI.getSourceSite();
			String crawlResult = crawlURI.getCrawlResult();
			List<CrawlURI> baikeImageCrawlURIList = (List<CrawlURI>)crawlURI.getModel();
			
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			String domain = PageProcessorUtils.getValue(domainReg, crawlUrl);
			
			// 替换
			String text = PageProcessorUtils.getValue(MODEL_LIST_REG, crawlResult);

			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(MODEL_REG, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(text);
			while (matcher.contains(input, pattern)) {
				try {
					MatchResult m = matcher.getMatch();
					String modelText = m.group(1);
					
					// 分析取得视频数据
					String oriImageUri = PageProcessorUtils.getValue(ORI_IMAGE_URL_REG,modelText);
					String name = PageProcessorUtils.getValue(NAME_REG,modelText);
					String category = PageProcessorUtils.getValue(CATEGORY_REG,modelText);
					String measure = PageProcessorUtils.getValue(MEASURE_REG,modelText);
					int width = -1;
					int height = -1;
					try {
						String[] measureSliper = measure.split("\\*");
						width = Integer.valueOf(measureSliper[0]);
						height = Integer.valueOf(measureSliper[1]);
					} catch ( Exception e ) {
						width = -1;
						height = -1;
					}
					
					if (width==-1) {
						System.out.println(measure);
						System.out.println(oriImageUri);
						System.out.println("");
					}
					if (StringUtils.isEmpty(oriImageUri)) {
						continue;
					}
					
					String size = PageProcessorUtils.getValue(SIZE_REG,modelText);
					
					if (StringUtils.isNotEmpty(oriImageUri) && !oriImageUri.startsWith("http://")) {
						oriImageUri = "http://" + domain + oriImageUri;
					}
					oriImageUri = oriImageUri.replaceAll("100X140", "o");
					
					BaikeImage bi = new BaikeImage();
					Baike baike = new Baike();
					baike.setSourceSite(sourceSite);
					baike.setOriId(oriId);
					bi.setBaike(baike);
					bi.setName(name);
					bi.setCategory(category);
					bi.setOriImageUri(oriImageUri);
					bi.setMeasure(measure);
					bi.setWidth(width);
					bi.setHeight(height);
					bi.setSize(size);
					bi.setStatus(Constants.IMAGE_STATUS_DEFAULT);
					bi.setCreateUser(crawlUser);
					bi.setCreateDate(crawlDate);
					bi.setUpdateUser(crawlUser);
					bi.setUpdateDate(crawlDate);
					
					CrawlURI baikeImageCrawlURI = new CrawlURI();
					baikeImageCrawlURI.setCrawlUrl(oriImageUri);
					baikeImageCrawlURI.setSourceSite(crawlURI.getSourceSite());
					baikeImageCrawlURI.setModel(bi);
					baikeImageCrawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);
					
					baikeImageCrawlURIList.add(baikeImageCrawlURI);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
			e.printStackTrace();
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
}
